##Robustness checks for conative attitudes for main endline and midline results

# lc1
lc1_el_nosize <- ols_main(
  outcome = "lc1",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = FALSE,
  cluster_SE = TRUE,
  covariates = NULL,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

lc1_el_pvals_nosize <- get_RI_pvals(
  outcome = "lc1",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = FALSE,
  cluster_SE = TRUE,
  covariates = NULL,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


lc1_ml_nosize <- ols_main(
  outcome = "lc1_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = FALSE,
  cluster_SE = TRUE,
  covariates = NULL,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

lc1_ml_pvals_nosize <- get_RI_pvals(
  outcome = "lc1_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = FALSE,
  cluster_SE = TRUE,
  covariates = NULL,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


# pta

pta_el_nosize <- ols_main(
  outcome = "pta",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = FALSE,
  cluster_SE = TRUE,
  covariates = NULL,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

pta_el_pvals_nosize <- get_RI_pvals(
  outcome = "pta",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = FALSE,
  cluster_SE = TRUE,
  covariates = NULL,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


pta_ml_nosize <- ols_main(
  outcome = "pta_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = FALSE,
  cluster_SE = TRUE,
  covariates = NULL,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

pta_ml_pvals_nosize <- get_RI_pvals(
  outcome = "pta_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = FALSE,
  cluster_SE = TRUE,
  covariates = NULL,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


# bring_up

bring_up_el_nosize <- ols_main(
  outcome = "bring_up",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = FALSE,
  cluster_SE = TRUE,
  covariates = NULL,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

bring_up_el_pvals_nosize <- get_RI_pvals(
  outcome = "bring_up",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = FALSE,
  cluster_SE = TRUE,
  covariates = NULL,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


bring_up_ml_nosize <- ols_main(
  outcome = "bring_up_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = FALSE,
  cluster_SE = TRUE,
  covariates = NULL,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

bring_up_ml_pvals_nosize <- get_RI_pvals(
  outcome = "bring_up_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = FALSE,
  cluster_SE = TRUE,
  covariates = NULL,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


# assemble


assemble_el_nosize <- ols_main(
  outcome = "assemble",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = FALSE,
  cluster_SE = TRUE,
  covariates = NULL,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

assemble_el_pvals_nosize <- get_RI_pvals(
  outcome = "assemble",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = FALSE,
  cluster_SE = TRUE,
  covariates = NULL,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


assemble_ml_nosize <- ols_main(
  outcome = "assemble_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = FALSE,
  cluster_SE = TRUE,
  covariates = NULL,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

assemble_ml_pvals_nosize <- get_RI_pvals(
  outcome = "assemble_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = FALSE,
  cluster_SE = TRUE,
  covariates = NULL,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")

# absenteeism_action (ml, el and total)
absenteeism_action_el_nosize <- ols_main(
	outcome = "absenteeism_action",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = FALSE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE)

absenteeism_action_el_pvals_nosize <- get_RI_pvals(
	outcome = "absenteeism_action",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = FALSE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

absenteeism_action_ml_nosize <- ols_main(
	outcome = "absenteeism_action_ml",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = FALSE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE)

absenteeism_action_ml_pvals_nosize <- get_RI_pvals(
	outcome = "absenteeism_action_ml",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = FALSE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

absenteeism_action_total_nosize <- ols_main(
	outcome = "absenteeism_action_total",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = FALSE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE)

absenteeism_action_total_pvals_nosize <- get_RI_pvals(
	outcome = "absenteeism_action_total",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = FALSE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")




#print robustness tables conative attitudes without audience size --------------------------------------------------
#setup
control_means <- with(
	el, 
	c(
		"Control Mean",
		round(mean(lc1_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(lc1[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(bring_up_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(bring_up[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(pta_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(pta[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(assemble_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(assemble[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(absenteeism_action_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(absenteeism_action[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(absenteeism_action_total[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2)
	)
)


pval_lines <- c(
	"RI $p$-values",
	round(lc1_ml_pvals_nosize$ri_pvals["absenteeism"],3),
	round(lc1_el_pvals_nosize$ri_pvals["absenteeism"],3),
	round(bring_up_ml_pvals_nosize$ri_pvals["absenteeism"],3),
	round(bring_up_el_pvals_nosize$ri_pvals["absenteeism"],3),
	round(pta_ml_pvals_nosize$ri_pvals["absenteeism"],3),
	round(pta_el_pvals_nosize$ri_pvals["absenteeism"],3),
	round(assemble_ml_pvals_nosize$ri_pvals["absenteeism"],3),
	round(assemble_el_pvals_nosize$ri_pvals["absenteeism"],3),
	round(absenteeism_action_ml_pvals_nosize$ri_pvals["absenteeism"],3), #note 3-digit p values 
	round(absenteeism_action_el_pvals_nosize$ri_pvals["absenteeism"],3),
	round(absenteeism_action_total_pvals_nosize$ri_pvals["absenteeism"],3)
)

hypothesis_lines <- c(
	"Hypothesis",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr"
)

#make tables
sink("03_tables/ABS_conative_attitudes_nosize.tex")
stargazer(
	... = list(
		lc1_ml_nosize$fit,
		lc1_el_nosize$fit,
		bring_up_ml_nosize$fit,
		bring_up_el_nosize$fit,
		pta_ml_nosize$fit,
		pta_el_nosize$fit,
		assemble_ml_nosize$fit,
		assemble_el_nosize$fit,
		absenteeism_action_ml_nosize$fit,
		absenteeism_action_el_nosize$fit,
		absenteeism_action_total_nosize$fit
	),
	type = "latex",
	p = list(
		lc1_ml_pvals_nosize$ri_pvals,
		lc1_el_pvals_nosize$ri_pvals,
		bring_up_ml_pvals_nosize$ri_pvals,
		bring_up_el_pvals_nosize$ri_pvals,
		pta_ml_pvals_nosize$ri_pvals,
		pta_el_pvals_nosize$ri_pvals,
		assemble_ml_pvals_nosize$ri_pvals,
		assemble_el_pvals_nosize$ri_pvals,
		absenteeism_action_ml_pvals_nosize$ri_pvals,
		absenteeism_action_el_pvals_nosize$ri_pvals,
		absenteeism_action_total_pvals_nosize$ri_pvals
	),
	se = list(
		lc1_ml_nosize$fit_summary[,"Std. Error"],
		lc1_el_nosize$fit_summary[,"Std. Error"],
		bring_up_ml_nosize$fit_summary[,"Std. Error"],
		bring_up_el_nosize$fit_summary[,"Std. Error"],
		pta_ml_nosize$fit_summary[,"Std. Error"],
		pta_el_nosize$fit_summary[,"Std. Error"],
		assemble_ml_nosize$fit_summary[,"Std. Error"],
		assemble_el_nosize$fit_summary[,"Std. Error"],
		absenteeism_action_ml_nosize$fit_summary[,"Std. Error"],
		absenteeism_action_el_nosize$fit_summary[,"Std. Error"],
		absenteeism_action_total_nosize$fit_summary[,"Std. Error"]
	),
	keep = "absenteeism",
	omit.stat = c("rsq","f","ser"),
	column.separate = c(2,2,2,2,3),
	column.labels = c("Involve LC1 Chair","Tell village","Use PTA","Assemble group", "Index"),
	table.layout = "=cd#-t-as=n",
	dep.var.labels = c("Midline","Endline",
										 "Midline","Endline",
										 "Midline","Endline",
										 "Midline","Endline",
										 "Midline","Endline","Overall"
	),
	dep.var.labels.include = TRUE,
	no.space = T,
	omit = "block_id",
	add.lines = list(
		control_means,
		pval_lines,
		hypothesis_lines,
		c("Block FE",
			"Yes","Yes","Yes",
			"Yes","Yes","Yes", 
			"Yes", "Yes",
			"Yes","Yes","Yes")),
	notes.label = "",
	float = FALSE
	# style = "qje"
)
sink()
